<?php
namespace app\home\logic;

use think\Model;
use app\home\logic\CartLogic;
use think\Db;

/**
 * 订单逻辑定义
 * Class CatsLogic
 * @package Home\Logic
 */
class OrderLogic extends Model {
    /**
     * 添加一个订单
     * @param $user_id|用户id
     * @param $order|订单
     * @param $address|地址
     * @param string $distribution_way|配送方式
     * @param string $user_note|用户备注
     * @return array
     */
    public function addOrder($user_id,$order,$address,$distribution_way,$user_note){

        // 0插入订单 order
        $data = array(
            'order_sn'         => date('YmdHis').rand(000000,999999), // 订单编号
            'user_id'          => $user_id, // 用户id
            'goods_price'      => $order['goods_price'] ,//'商品价格',
            'add_time'         =>time(), // 下单时间
            'username'         => $address['username'], // 联系人
            'phone'         => $address['phone'], // 电话
            'address'         => $address['region'].$address['address'], // 收货地址
            'distribution_way'         => $distribution_way, // 配送方式
            'user_note'         => $user_note, // 客户备注
        );
        $order_id = Db::name("shop_order")->insertGetId($data);
        if(!$order_id)
            return array('status' => 500,'msg'=>'添加订单失败','result'=> '');
        // 1插入order_goods 表
        $cartList = M('Cart')->where(['user_id'=>$user_id,'selected'=>1])->select();
        foreach($cartList as $key => $val){
            $data2['order_id']           = $order_id; // 订单id
            $data2['goods_id']           = $val['goods_id']; // 商品id
            $data2['goods_name']         = $val['goods_name']; // 商品名称
            $data2['goods_sn']           = $val['goods_sn']; // 商品货号
            $data2['goods_num']          = $val['goods_num']; // 购买数量
            $data2['goods_price']        = $val['goods_price']; // 商品价
            $data2['spec_key']           = $val['spec_key']; // 商品规格
            $data2['spec_key_name']      = $val['spec_key_name']; // 商品规格名称
            Db::name("OrderGoods")->insert($data2);
        }
        minus_stock($order_id);
        M('Cart')->where(['user_id' => $user_id,'selected' => 1])->delete();
        return array('status' => 200,'msg'=>'提交订单成功','result' => $order_id); // 返回新增的订单id
    }


    /**
     * 添加一个订单-直接购买支付
     * @param $user_id|用户id
     * @param $order|订单商品信息
     * @param $address|地址
     * @param string $distribution_way|配送方式
     * @param string $user_note|用户备注
     * @return array
     */
    public function addOrder2($user_id,$order,$address,$distribution_way,$user_note){

       $user =  Db::name("user")->where(['user_id'=>$user_id])->field(['vip','vip_time'])->find();
       $is_vip = strtotime($user['vip_time']) > time() && $user['vip'] ? true : false;
        //计算价格
        if ($order['spec_goods_price']['price'] != ''){ //有规格
            $goods_price = $is_vip ?  $order['goods_num'] * $order['spec_goods_price']['vip_price'] : $order['goods_num'] * $order['spec_goods_price']['price']; //规格价格
        }else{
            $goods_price = $is_vip ? $order['goods_num'] * $order['vip_price'] : $order['goods_num'] * $order['shop_price']; //商品本价
        }

        // 0插入订单 order
        $data = array(
            'order_sn'         => date('YmdHis').rand(000000,999999), // 订单编号
            'user_id'          => $user_id, // 用户id
            'goods_price'      => $goods_price ,//'商品价格',
            'add_time'         =>time(), // 下单时间
            'username'         => $address['username'], // 联系人
            'phone'         => $address['phone'], // 电话
            'address'         => $address['region'].$address['address'], // 收货地址
            'distribution_way'         => $distribution_way, // 配送方式
            'user_note'         => $user_note, // 客户备注
            'is_vip'            => $is_vip ? 1 : 0  //0是会员价格  1是会员价格
        );
        $order_id = Db::name("shop_order")->insertGetId($data);
        if(!$order_id) return array('status' => 500,'msg'=>'添加订单失败','result'=> '');

        //添加订单商品
        M('order_goods')->insert(array(
            'order_id' => $order_id,// 订单id
            'goods_id' => $order['goods_id'], // 商品id
            'goods_name' => $order['goods_name'], // 商品名称
            'goods_sn' => $order['goods_sn'], // 商品货号
            'goods_num' => $order['goods_num'], // 购买数量
            'goods_price' => $order['spec_goods_price']['price'] != '' ? $order['spec_goods_price']['price'] : $order['shop_price'], // 商品价
            'spec_key' => $order['spec_goods_price']['key']??'没有数据', // 商品规格
            'spec_key_name' => $order['spec_goods_price']['key_name'], // 商品规格名称
        ));
        minus_stock($order_id);
        return array('status' => 200,'msg'=>'提交订单成功','result' => $order_id); // 返回新增的订单id
    }


    /**
     * 订餐订单添加支付
     * @param $user_id|用户id
     * @param $order|订单商品信息
     * @param string $user_note|用户备注
     * @return array
     */
    public function addOrder3($user_id,$order,$user_note){

        // 0插入订单 order
        $data = array(
            'order_sn'         => date('YmdHis').rand(000000,999999), // 订单编号
            'user_id'          => $user_id, // 用户id
            'goods_price'      => $order['goods_price'] ,//'商品价格',
            'add_time'         =>time(), // 下单时间
            'user_note'         => $user_note, // 客户备注
        );
        $order_id = Db::name("meal_order")->insertGetId($data);
        if(!$order_id)
            return array('status' => 500,'msg'=>'添加订单失败','result'=> '');
        // 1插入order_goods 表
        $cartList = M('meal_cart')->where(['user_id'=>$user_id,'selected'=>1])->select();
        foreach($cartList as $key => $val){
            $data2['order_id']           = $order_id; // 订单id
            $data2['goods_id']           = $val['goods_id']; // 商品id
            $data2['goods_name']         = $val['goods_name']; // 商品名称
            $data2['goods_sn']           = $val['goods_sn']; // 商品货号
            $data2['goods_num']          = $val['goods_num']; // 购买数量
            $data2['goods_price']        = $val['goods_price']; // 商品价
            $data2['spec_key']           = $val['spec_key']; // 商品规格
            $data2['spec_key_name']      = $val['spec_key_name']; // 商品规格名称
            Db::name("meal_order_goods")->insert($data2);
        }
        minus_stock2($order_id);
        M('meal_cart')->where(['user_id' => $user_id,'selected' => 1])->delete();
        return array('status' => 200,'msg'=>'提交订单成功','result' => $order_id); // 返回新增的订单id
    }



    /*
     * 获取购物车中选中的商品
     * */
    public function getCartList($user_id){
        $cartList = DB::name('Cart')->where(['selected' =>1,'user_id' => $user_id])->select();  // 获取购物车商品
        $total_goods_num = $total_price = 0;//初始化数据。商品总共数量/商品总额
        foreach ($cartList as $k => $val) {
            $cartList[$k]['goods_fee'] = $val['goods_num'] * $val['goods_price'];
            $total_goods_num += $val['goods_num'];

            $total_price += $val['goods_num'] * $val['goods_price'];
        }
        $num = DB::name('Cart')->where(['selected' =>1,'user_id' => $user_id])->count();
        $total_price = array('total_fee' => $total_price, 'num' => $num); // 总计

        return array('cartList' => $cartList, 'total_price' => $total_price);
    }


    /*
    * 获取订餐购物车中选中的商品
    * */
    public function getCartList2($user_id){
        $cartList = DB::name('meal_cart')->where(['selected' =>1,'user_id' => $user_id])->select();  // 获取购物车商品
        $total_goods_num = $total_price = 0;//初始化数据。商品总共数量/商品总额
        foreach ($cartList as $k => $val) {
            $cartList[$k]['goods_fee'] = $val['goods_num'] * $val['goods_price'];
            $total_goods_num += $val['goods_num'];

            $total_price += $val['goods_num'] * $val['goods_price'];
        }
        $num = DB::name('meal_cart')->where(['selected' =>1,'user_id' => $user_id])->count();
        $total_price = array('total_fee' => $total_price, 'num' => $num); // 总计

        return array('cartList' => $cartList, 'total_price' => $total_price);
    }
}

